/* 
 * Copyright 2011 VelocitiSoftware
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.velociti.ikarus.widget.client.ui;

import java.util.Iterator;

import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.ui.VCheckBox;
import com.vaadin.terminal.gwt.client.ui.VOptionGroup;

/** ######VIkarusOptionGroup#####
 * @author Alper Turkyilmaz - VelocitiSoftware - 2011
 * @version 1.0.2
 */

public class VIkarusOptionGroup extends VOptionGroup implements FocusHandler, BlurHandler {
	public static final String HORIZONTAL = "horizontal";
	public static final String VERTICAL = "vertical";
	String alignment = HORIZONTAL;

	public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
		super.updateFromUIDL(uidl, client);
		if (uidl.getBooleanAttribute("cached")) {
			return;
		}
		String align = uidl.getStringAttribute("alignment");
		if (align != null) {
			alignment = align;
		}
		if (alignment.equals(HORIZONTAL)) {
			optionsContainer.removeStyleName(CLASSNAME);
			optionsContainer.addStyleName(CLASSNAME + "-" + alignment);
		}
	}

	protected void buildOptions(UIDL uidl) {
		super.buildOptions(uidl);
		if (alignment.equals(HORIZONTAL)) {
			for (Iterator iterator = ((Panel) optionsContainer).iterator(); iterator.hasNext();) {
				Widget child = (Widget) iterator.next();
				if (child instanceof RadioButton || child instanceof VCheckBox) {
					child.removeStyleName(CLASSNAME_OPTION);
					child.addStyleName(CLASSNAME_OPTION + "-" + alignment);
				}
			}
		}
	}

}
